import { _decorator, Component, Node, Camera, Canvas, Vec2, instantiate, Prefab, Layers, view, resources, game, director, find } from 'cc';
import { Bullet } from './Bullet';
import { BulletConfig } from './BulletConfig';
import { Enemy } from './Enemy';
import { EnemyConfig } from './EnemyConfig';
import { GameManager } from './GameManager';
import { UIConfig } from './UI/UIConfig';
import { UIMain } from './UI/UIMain';
const { ccclass, property } = _decorator;

@ccclass('UIManager')
export class UIManager extends Component
{
    public static instance: UIManager

    @property(Node)
    public FollowTarget: Node

    @property(Canvas)
    public canvas: Canvas

    public BulletPrefab: Prefab
    public BulletRoot: Node

    @property(Node)
    public UIRoot: Node = null
    @property(Node)
    public Camera: Node = null

    public MainUI:UIMain

    public EquitAreaItemPrefab: Prefab // 左上角显示装备的预制体


    onLoad()
    {
        UIManager.instance = this
        director.addPersistRootNode(this.node)
        
        resources.load("Prefabs/Bullet", Prefab, (err, prefab) => {
            this.BulletPrefab = prefab
        });
        resources.load("Prefabs/EquitAreaItem", Prefab, (err, prefab) => {
            this.EquitAreaItemPrefab = prefab
        });
    }

    TouchPosToUIPos(touchPos: Vec2): Vec2
    {
        return new Vec2(touchPos.x - this.canvas.node.getPosition().x, touchPos.y - this.canvas.node.getPosition().y)
    }

    GetCanvasSize(): Vec2
    {
        return new Vec2(view.getVisibleSize().width, view.getVisibleSize().height)
    }

    CreateBullet(config:BulletConfig)
    {
        let bullet = instantiate(this.BulletPrefab)
        bullet.getComponent(Bullet).Config = config
        bullet.parent = this.BulletRoot
        return bullet
    }

    OpenUI(e: UIConfig)
    {
        switch (e)
        {
            case UIConfig.Result:
                resources.load("Prefabs/UI/UIResult", Prefab, (err, prefab) =>
                {
                    let ui = instantiate(prefab)
                    ui.parent = this.UIRoot
                })
                break;
            case UIConfig.OpenChest:
                resources.load("Prefabs/UI/UIOpenChest", Prefab, (err, prefab) =>
                {
                    let ui = instantiate(prefab)
                    ui.parent = this.UIRoot
                })
                break;
            case UIConfig.GameOver:
                resources.load("Prefabs/UI/UIGameOver", Prefab, (err, prefab) =>
                {
                    let ui = instantiate(prefab)
                    ui.parent = this.UIRoot
                })
                break;
            default:
                break;
        }
    }

    update()
    {
        if (this.UIRoot != null) {
            this.UIRoot.setPosition(this.FollowTarget.getPosition())
        }
        if (this.Camera != null) {
            this.Camera.setPosition(this.FollowTarget.getPosition())
        }
    }

    OnGameStart()
    {
        this.BulletRoot = find("Canvas/BulletRoot")
        this.canvas = find("Canvas").getComponent(Canvas)
        this.UIRoot = find("Canvas/UIRoot")
        this.Camera = find("Canvas/Camera")
        this.FollowTarget = find("Canvas/Player")
        this.MainUI = find("Canvas/UIRoot/UIMain").getComponent(UIMain)
    }

    GetMainUI()
    {
        return this.MainUI
    }
}



